<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\CategoryListParams;
use App\Params\Backend\CategoryUpdateParam;
use App\Services\Goods\CategoryService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 商品分类控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class CategoryController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {

    }

    /**
     * 保存分类
     *
     * @throws
     * @return
     */
    public function update()
    {
        $param = CategoryUpdateParam::create();
        $ret = CategoryService::getInstance()->update($param);
        return $this->success(['category_id' => $ret]);
    }

    /**
     * 商品分类列表
     *
     * @throws
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $param = CategoryListParams::create();
        $categories = CategoryService::getInstance()->getList($param);
        return $this->success($categories);
    }

    /**
     * 根据层级获取分类
     *
     * @param Request $request
     * @return
     */
    public function getChild(Request $request)
    {
        $level = $request->input('level', 0);
        $ret = CategoryService::getInstance()->getChild($level);
        return $this->success($ret);
    }

    /**
     * 删除分类
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function delete(Request $request)
    {
        $categoryId = $request->input('category_id', 0);
        $ret = CategoryService::getInstance()->delete($categoryId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::CATEGORY_DELETE_FAIL);
    }

    /**
     * 获取分类
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function get()
    {
        $id = $this->verifyInteger('id');
        $ret = CategoryService::getInstance()->get($id);
        return $this->success($ret);
    }

    /**
     * 停用/启用分类
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function enable()
    {
        $id = $this->verifyInteger('id');
        $ret = CategoryService::getInstance()->enable($id);
        return $this->success($ret);
    }
}
